package com.smedi.ismedi.dangeroussourcemanage.core.domain.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.FileCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceLibrary;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceManage;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceLibraryRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.service.DangerousSourceManageDomainService;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.service.ExportExcelDomainService;
import com.smedi.ismedi.dangeroussourcemanage.core.infrastructure.constant.DangerousSourceManageError;
import com.smedi.ismedi.dangeroussourcemanage.core.infrastructure.util.ExportFiles;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.model.DangerousSourceManageEntity;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.mybatis.mapper.DangerousSourceManageMapper;
import com.smedi.ismedi.web.starter.exception.LogicException;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务服务实体类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Slf4j
@Component
@AllArgsConstructor
public class ExportExcelDomainServiceImpl extends ExportFiles implements ExportExcelDomainService {
    private final DangerousSourceLibraryRepository dangerousSourceLibraryRepository;

    private final DozerUtil dozerUtil;

    @Override
    public ResponseEntity<byte[]> exportDangerousSourceList(List<DangerousSourceManage> dangerousSourceManageList) {
        try {
            FileCommand fileCommand = creatExcel("fileTemplate/dangerousExportListTemplate.xlsx");
            XSSFSheet buildingSheet = fileCommand.getSheet();
            XSSFWorkbook workBook = fileCommand.getWorkbook();
            //获取模板文件的行，通过行再获取到相应的单元格cell,设置cell的值。
            int i = 0;
            for (DangerousSourceManage dangerousSourceManage : dangerousSourceManageList) {
                i++;
                XSSFRow rowAdd = buildingSheet.createRow(i);
                for (int j = 0; j < 18; j++) {
                    rowAdd.createCell(j);
                }
                rowAdd.getCell(0).setCellValue(dangerousSourceManage.getConstructionUnit());
                rowAdd.getCell(1).setCellValue(dangerousSourceManage.getProjectName());
                rowAdd.getCell(2).setCellValue(dangerousSourceManage.getUnitEngineering());
                rowAdd.getCell(3).setCellValue(i);
                rowAdd.getCell(4).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getEngineeringCategory());
                rowAdd.getCell(5).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getConstructionPhase());
                rowAdd.getCell(6).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getSubItemProjectName());
                rowAdd.getCell(7).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getExpertReviewSubItem());
                rowAdd.getCell(8).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getConstructionContent());
                rowAdd.getCell(9).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getKeyHazards());
                rowAdd.getCell(10).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getProneAccident());
                rowAdd.getCell(11).setCellValue(dangerousSourceManage.getDangerousSourceLibrary().getMeasures());
                rowAdd.getCell(12).setCellValue(dangerousSourceManage.getProjectAddress());
                rowAdd.getCell(13).setCellValue(dangerousSourceManage.getProjectManager());
                rowAdd.getCell(14).setCellValue(dangerousSourceManage.getMobile());
                rowAdd.getCell(15).setCellValue(dangerousSourceManage.getPlanConstructionTime().toString().substring(0,10));
                rowAdd.getCell(16).setCellValue(dangerousSourceManage.getPlanEndTime().toString().toString().substring(0,10));
                LocalDateTime startTime = dangerousSourceManage.getPlanConstructionTime();
                LocalDateTime endTime = dangerousSourceManage.getPlanEndTime();
                LocalDateTime nowTime = LocalDateTime.now();
                if (nowTime.isBefore(startTime)) {
                    rowAdd.getCell(17).setCellValue("未实施");
                } else if (nowTime.isAfter(startTime) && nowTime.isBefore(endTime)) {
                    rowAdd.getCell(17).setCellValue("正在实施");
                } else if (nowTime.isAfter(endTime)) {
                    rowAdd.getCell(17).setCellValue("实施完成");
                }
            }


            workBook.removeSheetAt(0);
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            workBook.write(outByteStream);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", new String("危险源列表".getBytes(), StandardCharsets.ISO_8859_1) + ".xlsx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(outByteStream.toByteArray(), headers, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }
}
